package com.whz.mutithread;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName LazyInit
 * @Description TODO 实现延迟初始化
 * @Author hong-zhi
 * @Date 2021/5/26 15:27
 * @Version 1.0
 */
@Slf4j
public class LazyInit {
    private FieldType field =  computeFieldValue();
    /**
     * 对静态域使用延迟初始化,就使用 lazy initialization holder class模式
     * 保证类要被用到的时候才会被初始化
     */
    private static class FieldHolder {
        static final FieldType field = computeFieldValue();
    }
    private static FieldType getField() {
        return FieldHolder.field;
    }
    private static FieldType computeFieldValue() {
        return new FieldType();
    }

    public static void main(String[] args) {
//        for (int i = 0; i < 10; i++) {
//            FieldType field = getField();
//            log.info("field: {}", field);
//        }
        LazyInit li = new LazyInit();


    }
}
